package de.fmaul.dicheck.reporting;

import java.util.HashMap;
import java.util.Map;

import de.fmaul.dicheck.model.TestCase;
import de.fmaul.dicheck.model.TestContainer;
import de.fmaul.dicheck.runner.TestResult;
import de.fmaul.dicheck.runner.TestRunnerResult;

public class Statistics {

    public Statistics(final TestContainer container,
            final TestRunnerResult runnerResult) {
        
        
        calculateTotals(container, runnerResult, totals);
    }
    
    /**
     * @author florian 
     * Must be public for freemarker to use it as method return
     *         object!
     */
    public class Totals {
        private final int successCount;
        private final int failedCount;

        public Totals(final int successCount, final int failedCount) {
            this.successCount = successCount;
            this.failedCount = failedCount;
        }

        public int getSuccessCount() {
            return successCount;
        }

        public int getFailedCount() {
            return failedCount;
        }
    }
    
    final Map<String, Totals> totals = new HashMap<String, Totals>();

    public Totals getTotals(String containerId) {
        return totals.get(containerId);
    }

    private Totals calculateTotals(final TestContainer container,
            final TestRunnerResult runnerResult,
            final Map<String, Totals> totals) {
        int success = 0;
        int failed = 0;

        for (final TestContainer tc : container.getSubContainers()) {
            final Totals total = calculateTotals(tc, runnerResult, totals);
            success += total.getSuccessCount();
            failed += total.getFailedCount();
        }
        for (final TestCase t : container.getTestCases()) {
            final TestResult result = runnerResult.getTestResult(t);
            switch (result.getOutcome()) {
            case PASSED:
                success++;
                break;
            case FAILED:
                failed++;
                break;
            }

        }

        final Totals total = new Totals(success, failed);
        totals.put(container.getId(), total);
        return total;
    }
    
    
}
